﻿using Furion.DatabaseAccessor;
using iemtm.WeChat.Core.Model;
using iemtm.WeChat.Core.Util;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iemtm.WeChat.Core.Quartz.Job
{
    /// <summary>
    /// 推送铁塔工单审核
    /// </summary>
    [DisallowConcurrentExecution]//前一次任务未执行完成时不触发下次任务
    public class WxPushOperatorAnnounceAudit : IJob
    {
      
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                try
                {
                    int? Count = 0;
                    //获取铁塔工单推送区域数量统计
                    List<OperatorAnnounceAuditModel> OperatorAnnounceAuditList = Common.GetOperatorAnnounceAudit();
                    if (OperatorAnnounceAuditList == null)
                        return;
                    //找出需要推送人员
                    //var userList = dbContext.Queryable<UserAccount>().Where(u => u.RoleCode == "subadmin" && u.PrjCode == "0113" && u.WxOpenId != null).ToList();
                    //var userList = dbContext.Queryable<UserAccount, Company>((u, c) => new object[] { JoinType.Left, u.CompanyCode == c.CompanyCode }).Where((u, c) => (u.RoleCode == "ChinaMobileMonitor" || u.RoleCode == "ChinaUnicomMonitor" || u.RoleCode == "ChinaTelcomMonitor") && u.PrjCode == "0113" && u.WxOpenId != null).Select((u, c) => new { UserCode = u.UserCode, AreaCode = c.AreaCode, RoleCode = u.RoleCode }).ToList();
                    var userList = Common.GetWxUserCompanyModel();
                    if (userList == null)
                        return;
                    foreach (var item in userList)
                    {
                        try
                        {
                            switch (item.RoleCode)
                            {
                                case "ChinaMobileMonitor":
                                    //if (item.UserCode=="gzyd")
                                    //{
                                    Count = OperatorAnnounceAuditList.FirstOrDefault(t => t.AreaCode == item.AreaCode).MobileCount;
                                    //执行推送方法
                                    WeiXinPush.SendAnnounceAuditMsg(item.UserCode, Count);
                                    //}
                                    break;
                                case "ChinaUnicomMonitor":
                                    //if (item.UserCode=="gzyd")
                                    //{
                                    Count = OperatorAnnounceAuditList.FirstOrDefault(t => t.AreaCode == item.AreaCode).UnicomCount;
                                    //执行推送方法
                                    WeiXinPush.SendAnnounceAuditMsg(item.UserCode, Count);
                                    //}
                                    break;
                                case "ChinaTelcomMonitor":
                                    //if (item.UserCode=="gzyd")
                                    //{
                                    Count = OperatorAnnounceAuditList.FirstOrDefault(t => t.AreaCode == item.AreaCode).TelecomCount;
                                    //执行推送方法
                                    WeiXinPush.SendAnnounceAuditMsg(item.UserCode, Count);
                                    //}
                                    break;
                                default:
                                    break;
                            }
                        }
                        catch (Exception e)
                        {
                            Db.GetRepository<Log>().Insert(new Log() { LogName = "运营商审核工单推送失败", LogDetails = e.ToString(), CreateTime = DateTime.Now });
                            continue;
                        }
                    }

                }
                catch (Exception e)
                {
                    Db.GetRepository<Log>().Insert(new Log() { LogName = "运营商审核工单推送失败", LogDetails = e.ToString(), CreateTime = DateTime.Now });
                }
            });
        }
    }
}
